package cn.wk.pet.service.impl;

import cn.wk.basic.constant.ConstantPethome;
import cn.wk.basic.service.impl.BaseServiceImpl;
import cn.wk.basic.utils.CodeGenerateUtils;
import cn.wk.order.domain.PetAcquisitionOrder;
import cn.wk.order.mapper.PetAcquisitionOrderMapper;
import cn.wk.org.domain.Employee;
import cn.wk.org.mapper.EmployeeMapper;
import cn.wk.pet.domain.Pet;
import cn.wk.pet.domain.PetDetail;
import cn.wk.pet.domain.SearchMasterMsg;
import cn.wk.pet.mapper.PetDetailMapper;
import cn.wk.pet.mapper.PetMapper;
import cn.wk.pet.mapper.SearchMasterMsgMapper;
import cn.wk.pet.service.IPetService;
import cn.wk.pet.service.ISearchMasterMsgService;
import cn.wk.user.domain.LoginInfo;
import com.fasterxml.jackson.databind.annotation.JsonAppend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PetAcquisitionOrderMapper petAcquisitionOrderMapper;

    /**
     * 处理待处理宠物列表中的数据把其保存到pet表中
     * @param pet
     */
    @Override
    @Transactional
    public void handleSave(Pet pet,LoginInfo loginInfo) {
        //添加宠物来源信息
        pet.setSourceType(ConstantPethome.FOR_THE_LORD);
        //保存pet信息
        petMapper.save(pet);
        //添加宠物简介和领养须知
        PetDetail detail = pet.getDetail();
        detail.setPet(pet);
        petDetailMapper.save(detail);
        //更改寻主信息表中的状态，state变成1
        Long id = pet.getSearchMasterMsg().getId();
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.findById(id);
        searchMasterMsg.setState(ConstantPethome.TO_DEAL_WITH);
        searchMasterMsgMapper.updateByPet(searchMasterMsg);
        //根据登录人信息查找员工
        Employee employee = employeeMapper.findByLogininfoIdGetEmp(loginInfo.getId());
        //创建要保存到t_order_pet_acquisition表中的信息（创建收购订单）
        PetAcquisitionOrder petAcquisitionOrder = createdPetAcquisitionOrder(employee,pet,searchMasterMsg);
        //保存订单
        petAcquisitionOrderMapper.save(petAcquisitionOrder);
    }

    /**
     * 创建收购订单
     * @param employee
     * @param pet
     * @param searchMasterMsg
     * @return
     */
    private PetAcquisitionOrder createdPetAcquisitionOrder(Employee employee, Pet pet, SearchMasterMsg searchMasterMsg) {
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        //设置digest摘要
        order.setDigest("[摘要]对"+searchMasterMsg.getName()+"收购订单！");
        //设置订单状态state 1 待报账(垫付) 2 待打款(银行转账)  3 完成
        order.setState(pet.getCostprice().equals(BigDecimal.ZERO)?ConstantPethome.ACQUISITION_ORDER_COMPLETE:pet.getPaytype());
        //设置收购价格 price
        order.setPrice(pet.getCostprice());
        //设置收购地址 address
        order.setAddress(searchMasterMsg.getAddress());
        //设置订单编号orderSn 这里用到一个工具类
        order.setOrderSn(CodeGenerateUtils.generateOrderSn(pet.getUser().getId()));
        //设置订单时间 lastcomfirmtime
        order.setLastcomfirmtime(order.getState()==ConstantPethome.ACQUISITION_ORDER_COMPLETE?new Date():null);
        //设置宠物 pet
        order.setPet(pet);
        //设置信息发布者 user
        order.setUser(pet.getUser());
        //设置支付类型
        order.setPaytype(pet.getPaytype());
        //设置shop
        order.setShop(pet.getShop());
        //设置员工
        order.setEmployee(employee);
        return order;
    }

    /**
     * 保存自己添加宠物
     * @param pet
     */
    @Override
    @Transactional
    public void savePet(Pet pet) {
        //先保存pet表中的数据
        petMapper.save(pet);
        //再保存detail中的数据
        PetDetail detail = pet.getDetail();
        detail.setPet(pet);
        petDetailMapper.save(detail);
    }

    /**
     * 修改添加的宠物
     * @param pet
     */
    @Override
    @Transactional
    public void update(Pet pet) {
        super.update(pet);
        //保存其关联的宠物介绍和领养须知
        PetDetail detail = pet.getDetail();
        detail.setPet(pet);
        petDetailMapper.update(detail);
    }
}
